package com.hs.core.utils;

import java.util.Iterator;
import java.util.List;
 

public class BasePage implements IPage {

	  /**
	 * 
	 */
	private static final long serialVersionUID = 4908196407040898831L;

	protected List elements; 

      protected int pageSize; 

      protected int pageNumber; 

      protected int totalElements = 0; 

      public BasePage(int pageNumber, int pageSize, int totalElements) { 
              this.pageNumber = pageNumber; 
              this.pageSize = pageSize; 
              this.totalElements = totalElements; 
              recomputePageNumber(); 
      } 

      public BasePage(int pageNumber, int pageSize) { 
              this.pageNumber = pageNumber; 
              this.pageSize = pageSize; 
      } 

      /** 
      * 重新计?愕鼻耙3的号码 
      */ 
      protected void recomputePageNumber() { 
              if (Integer.MAX_VALUE == this.pageNumber 
                              || this.pageNumber > getLastPageNumber()) { //last page 
                      this.pageNumber = getLastPageNumber(); 
              } 
      } 

      public boolean isFirstPage() { 
              return getThisPageNumber() == 1; 
      } 

      public boolean isLastPage() { 
              return getThisPageNumber() >= getLastPageNumber(); 
      } 

      public boolean hasNextPage() { 
              return getLastPageNumber() > getThisPageNumber(); 
      } 

      public boolean hasPreviousPage() { 
              return getThisPageNumber() > 1; 
      } 

      public int getLastPageNumber() { 
              return totalElements % this.pageSize == 0 ? 
                              totalElements/ this.pageSize 
                              : totalElements / this.pageSize + 1; 
      } 

      public List getThisPageElements() { 
              return elements; 
      } 

      public int getTotalNumberOfElements() { 
              return totalElements; 
      } 

      public int getThisPageFirstElementNumber() { 
              return (getThisPageNumber() - 1) * getPageSize() + 1; 
      } 

      public int getThisPageLastElementNumber() { 
              int fullPage = getThisPageFirstElementNumber() + getPageSize() - 1; 
              return getTotalNumberOfElements() < fullPage ? getTotalNumberOfElements() : fullPage; 
      } 

      public int getNextPageNumber() { 
              return getThisPageNumber() + 1; 
      } 

      public int getPreviousPageNumber() { 
              return getThisPageNumber() - 1; 
      } 

      public int getPageSize() { 
              return pageSize; 
      } 

      public int getThisPageNumber() { 
              return pageNumber; 
      } 
      /** 
      * 实现Iterable遍历接口 
      * @return 等价((Iterable)getThisPageElements()).iterator() 
      * @throws UnsupportedOperationException 
      *                    当getThisPageElements()不是Iterable接口 
      * @See java.lang.Iterable 
      */ 
      public Iterator iterator() { 
    	    return getThisPageElements().iterator();
//              if(getThisPageElements() instanceof Iterable) { 
//                      return ((Iterable)getThisPageElements()).iterator(); 
//              } 
//              else { 
//                      throw new UnsupportedOperationException("iterator() method is unsupported"); 
//              } 
      }

}
